/*
 * Author:  Ramachiatanya Kavuluru (kavuluru@usc.edu)
 *
 * Title :  my570list.cpp
 *
 */


#include<iostream>
#include "my570list.hpp"
using namespace std;


// Constructor and destructor for My570List: Begin

My570List::My570List()
{
	
	num_members=0;
	
	anchor=new My570ListElem();
	
	anchor->next= NULL;
	anchor->prev= NULL; 
	anchor->obj=NULL;
	
}

My570List::~My570List()
{
}

// Constructor and destructor for My570List: End



My570ListElem::My570ListElem(void*object)
{
	this->Set(object); 
}

My570ListElem::~My570ListElem()
{
}

My570ListElem * My570List :: First()
{
	
	
	if(Length()==0)
	{
		return NULL;
	}
	else
	{
		return anchor->next;
	}
	
	
} 

My570ListElem * My570List :: Last()
{
	
	if(Length()==0)
	{
		return NULL;
	}
	else
	{
		return anchor->prev;
	} 
}


My570ListElem * My570List :: Next(My570ListElem *elem)
{
	
	if(Length()==0)
	{
		return NULL;
	}
	else
	{
		if(elem->next==anchor)
		{
			return NULL;
		}
		else
		{
			return elem->next;
		}
	}
	
	
}

My570ListElem * My570List :: Prev(My570ListElem *elem)
{  
	
	
	
	if(Length()==0)
	{
		return NULL;
	}
	else
	{
		if(elem->prev == anchor)
		{
			return NULL;
		}
		else
		{
			return elem->prev;
		}
	}
	
}

int My570List :: InsertBefore(void *obj, My570ListElem *elem)
{
	
	My570ListElem *elem1 = new My570ListElem();
	
	
	
	
	
    elem1->Set(obj);
    if(elem==First() || elem==NULL)
    {
		Prepend(obj);
    }
    else
    {
		elem1->Set(obj);
		elem1->next=elem;
		elem1->prev=elem->prev; 
		elem->prev->next=elem1;
		elem->prev=elem1;
    
    num_members=num_members+1;
	}
	
	
    return TRUE;
	
}

int My570List :: InsertAfter(void *obj, My570ListElem *elem)
{
	My570ListElem *elem2 =new My570ListElem();
	
    elem2->Set(obj);
    if(elem==Last() || elem==NULL)
    {
		Append(obj);   
	}   
	
    else
    {
		elem2->Set(obj);
		elem2->prev=elem;
		elem2->next=elem->next;
		elem->next->prev=elem2;
		elem->next=elem2;
  
    num_members=num_members+1;
	}
	
    return TRUE;
	
}

int My570List::Append(void *obj)
{
	
	My570ListElem *elem=new My570ListElem();
	
	elem->Set(obj); 
	
	if(Last()==NULL)
	{
		
		
		elem->next=anchor;
		elem->prev=anchor;
		anchor->next=elem;
		anchor->prev=elem;
		
		
	}
	else
	{
		
		elem->next=anchor;
		Last()->next=elem;
		elem->prev=Last();
		anchor->prev=elem; 
		
	}
	
	
	
	num_members=num_members+1; 
	return TRUE;
	
} 




int My570List::Prepend(void *obj)
{
	
	My570ListElem *elem=new My570ListElem();
	
	elem->Set(obj); 
	
	if(First()==NULL)
	{ 
		elem->next=anchor;
		elem->prev=anchor;
		anchor->next=elem;
		anchor->prev=elem;
	}
	else
	{
		elem->prev=anchor;
		
		First()->prev=elem;
		elem->next=First();
		anchor->next=elem; 
	}
	
	num_members=num_members+1;
	return TRUE;
	
}

void My570List :: Unlink (My570ListElem *elem)
{
	if(Length()==0)
	{
		return;
	}
	
	else
	{

			(elem->prev)->next=elem->next;
			(elem->next)->prev=elem->prev;
			
			elem->next=NULL;
			elem->prev=NULL;
			delete elem;
			
			num_members=num_members-1;

	}
	
}


void My570List:: UnlinkAll()
{
	if(Length()==0)
	{
		return;
	}
	else
	{
		My570ListElem *elem = new My570ListElem();
		
		
		
		for(elem=First();elem!=NULL;elem=Next(elem))
		{
			Unlink(elem);
			
		}
		return;
	} 
	
}


My570ListElem * My570List :: Find(void * obj)
{
	My570ListElem *elem = new My570ListElem();
	
	
	for(elem=First();elem!=NULL;elem=Next(elem))
	{
		if(elem->Obj()==obj)
		{
			return elem;
		}
		
	}
	
	
	cout<<"The object does not exist in the list"<<endl;
	return NULL;   
	
	
	
}


/*
 int main()
 {
 
 My570List *mfl= new My570List();
 My570ListElem *elem= new My570ListElem();
 
 int num_items=20;
 int i=0,j=0,ival=0;
 
 for (i=0; i < num_items; i++) {
 mfl->Append((void*)i);
 }
 
 //for(elem=mfl->First();elem!=NULL;elem=mfl->Next(elem))
 
 elem=mfl->First();
 for(i=0;i<10;i++)
 {
 
 elem->Obj();
 
 elem=mfl->Next(elem);
 cout<<j+1;
 
 }
 
 mfl->UnlinkAll();
 
 cout<<"Contact ET"<<endl;
 
 
 mfl->Unlink(mfl->First());
 mfl->Unlink(mfl->First());
 mfl->Unlink(mfl->First());  
 mfl->Unlink(mfl->First());
 
 cout<<endl;
 cout<<endl;
 for (elem=mfl->First(); elem != NULL; elem=mfl->Next(elem)) {
 ival=(int)(elem->Obj());
 
 cout<<ival<<endl;
 }
 cout<<"Hello"<<endl;
 
 // mfl->UnlinkAll();
 
 
 
 
 int z=909;
 int h=7086;
 int f=687;
 int g=1234;
 int s=9861;
 
 mfl->Prepend((void*)z);
 mfl->Prepend((void*)h);   
 mfl->Append((void*)f);
 mfl->Prepend((void*)g);
 
 
 
 for (elem=mfl->First(); elem != NULL; elem=mfl->Next(elem)) {
 ival=(int)(elem->Obj());
 
 cout<<ival<<endl;
 }
 
 cout<<endl; 
 mfl->InsertAfter((void *)f,mfl->First());  mfl->Prepend((void*)s);
 mfl->InsertBefore((void *)s,mfl->First());
 mfl->Append((void *)g);
 
 
 for (elem=mfl->First(); elem != NULL; elem=mfl->Next(elem)) {
 ival=(int)(elem->Obj());
 
 cout<<ival<<endl;    
 
 }
 My570ListElem *elemq=new My570ListElem();
 elemq=mfl->Find((void*)909);
 ival=(int)(elemq->Obj());
 cout<<ival<<endl;
 
 
 
 delete mfl;
 return 0;
 }
 */

